package com.hqyj.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hqyj.config.AliyunConfig;
import com.hqyj.module.login.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;


@Component
public class MySms {

    @Autowired
    AliyunConfig aliyunConfig;

    //注入redis
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    public HashMap<String,Object> sendSms(String userName){
        String tel = userName;
        HashMap<String,Object> map = new HashMap<>();
        //cn-hangzhou 是阿里定义的签名固定值。填写阿里云申请短信签名的key和secret值
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunConfig.getKey(), aliyunConfig.getScrect());
        /*阿里云提供的短信发送api的近期的相关代码，代码以最新的阿里api提供为准*/
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        //接收电话
        request.putQueryParameter("PhoneNumbers",tel);
        //短信签名
        request.putQueryParameter("SignName",aliyunConfig.getSignName());
        //短信模板
        request.putQueryParameter("TemplateCode",aliyunConfig.getTemplateCode());
        //短信内容   随机数
        Random random = new Random();
        int num = random.nextInt(9000)+1000;
        request.putQueryParameter("TemplateParam","{code:"+num+"}");

        //获取响应结果
        try {
            CommonResponse commonResponse = client.getCommonResponse(request);
            System.out.println(commonResponse.getData().toString());
            //把JSON格式的字符串转换成map的数据类型。
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                HashMap hashMap = objectMapper.readValue(commonResponse.getData().toString(), HashMap.class);
                if (hashMap.get("Message").equals("OK")){
                    redisTemplate.opsForValue().set(tel,num+"",3, TimeUnit.MINUTES);
                    map.put("info","短信发送成功！");
                }
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                map.put("info","短信发送失败！");
            }

        } catch (ClientException e) {
            e.printStackTrace();
        }
        return map;
    }
    public HashMap<String, Object> smsLoginRedis(UserInfo userInfo) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        String code1 = redisTemplate.opsForValue().get(userInfo.getUserName())+"";
        if(userInfo.getUserPwd().equals(code1)){
            map.put("info","短信登录成功");
        }else{
            map.put("info","验证码输入不正确");
        }
        return map;
    }
}
